import * as validators from '@vuelidate/validators'
import i18n from "@/i18n"

const { t } = i18n.global

// or import { createI18nMessage } from '@vuelidate/validators'
const { createI18nMessage } = validators
const messagePath = ({ $validator }: any) => `messages.${$validator}`

export const withI18nMessage = createI18nMessage({ t, messagePath })

export const generateValidator = (validatorRules: any, validatorMessage?: string) => {
    console.log(typeof validatorRules)
    if(!validatorRules) return
    if(typeof validatorRules === 'string') {
        console.log('validatorRules', validatorRules)
        console.log('validators', validators[validatorRules](3))
        
        return withI18nMessage(() => true, getMessagePath(validatorMessage))
    }
    if(typeof validatorRules == 'function') return withI18nMessage(validatorRules, getMessagePath(validatorMessage))
    if(typeof validatorRules == 'object') {}
}

export const getMessagePath = (validatorMessage: string | undefined) => {
    return { messagePath: () => validatorMessage ? validatorMessage : 'messages.default' }
}
// Create your i18n message instance. Used for vue-i18n@9
// const withI18nMessage = createI18nMessage({ t: i18n.global.t.bind(i18n) })
// for vue-i18n@8
// const withI18nMessage = createI18nMessage({ t: i18n.t.bind(i18n) })

// // wrap each validator.
// export const required = withI18nMessage(validators.required)
// // validators that expect a parameter should have `{ withArguments: true }` passed as a second parameter, to annotate they should be wrapped
// export const minLength = withI18nMessage(validators.minLength, { withArguments: true })
// // or you can provide the param at definition, statically
// export const maxLength = withI18nMessage(validators.maxLength(10))